package me.twoday.library.mine.handler;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 상속받아 SAX를 최종구현하기위한 헨들러 클래스
 *
 * @author 김학성
 * @since 2012. 5. 18.
 */
public abstract class SaxDataHandler<T> extends DefaultHandler
{
	public String elementValue = null;
	public Boolean elementOn = false;
	public ArrayList<T> dataList = new ArrayList<T>();
	public T data = null;

	public ArrayList<T> getDataList() {
		return dataList;
	}

	public abstract void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException; 
	/*{
        elementOn = true; 
        if(localName.equals("CD"))
        {
            data = new T();
        }       
    }*/

	/**
	 * This will be called when the tags of the XML end.
	 **/
	@Override
	public abstract void endElement(String uri, String localName, String qName) throws SAXException;
	/*{
		elementOn = false;

		if(localName.equalsIgnoreCase("title"))
		{
			data.setTitle(elementValue);
		} 
		else if(localName.equalsIgnoreCase("artist"))
		{
			data.setArtist(elementValue);
		}
		else if(localName.equalsIgnoreCase("country"))
		{
			data.setCountry(elementValue);
		}
		else if(localName.equalsIgnoreCase("company"))
		{
			data.setCompany(elementValue);
		}
		else if(localName.equalsIgnoreCase("cd"))
		{
			dataList.add(data);
			data = null;
		}
	}*/

	/**
	 * This is called to get the tags value
	 **/
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (elementOn) {
			elementValue = new String(ch, start, length);
			elementOn = false;
		}
	}
}
